package com.ty.day3.home;

import java.util.Random;
import java.util.Scanner;

import static java.lang.Thread.sleep;

/**
 * Created by IntelliJ IDEA.
 *
 * @author : txy
 * @create : 2023/11/16 18:20
 */
public class Poker {
    private String[] color ;
    private String[] count;
    private String[] cards;
    public Poker(){

        color= new String[]{"♠", "♥", "♣", "♦"};
        count= new String[]{"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        cards = new String[54];
        int x=0;
        for (int i = 0; i < color.length; i++) {
            for (int j = 0; j < count.length; j++) {
                cards[x++]=color[i]+count[j];
            }
        }
        cards[x++]="🃏";
        cards[x]="🃏";
//        System.out.println("请检查牌是否有问题：");
//        for (int i = 0; i < cards.length; i++) {
//            System.out.print(cards[i]+"\t");
//            if ((i+1)%13==0){
//                System.out.println();
//            }
//        }
    }
    public String[] shuffle(){
        Random random = new Random();
        for (int i = 0; i < cards.length; i++) {
            int x = random.nextInt(54);
            String t=cards[i];
            cards[i] = cards[x];
            cards[x]=t;
        }
        return cards;
    }
    public String[][] deal(int number,String[] poker){
        if (poker.length!=54){
            return null;
        }
        int p=0,x=poker.length/number;
        String[][] result=new String[number+1][];
        for (int i = 0; i < result.length-1; i++) {
            result[i] = new String[x];
        }
        p=0;
        for (int i = 0; i < result[0].length; i++) {
            for (int j = 0; j < number; j++) {
                result[j][i]=poker[p];
                p++;
            }
        }

        result[number]=new String[poker.length-(x*number)];
        for (int i = 0; i < result[number].length; i++) {
            result[number][i]=poker[p++];
        }
        return result;
    }
}
class Player{
    public static void main(String[] args) throws InterruptedException {
        Poker poker = new Poker();
        String[] sPoker = poker.shuffle();
        System.out.println("洗牌中...");
        for (int i = 0; i < sPoker.length; i++) {
            System.out.print(sPoker[i]+"\t");
            if ((i+1)%13==0){
                System.out.println();
            }
        }
        System.out.println("\n\n输入玩家数：");
        Scanner sc = new Scanner(System.in);
        int player = sc.nextInt();
        String[][] deal = poker.deal(player, sPoker);
        Player.dealCards(deal);
    }
    public static void dealCards(String[][] deal){
        for (int i = 0; i < deal.length; i++) {
            if (i==deal.length-1){
                System.out.println("\n剩余牌：");
                for (int j = 0; j < deal[i].length; j++) {
                    System.out.print(deal[i][j]+"\t");
                }
                continue;
            }
            System.out.println("\n第"+(i+1)+"位玩家手牌");
            for (int j = 0; j < deal[i].length; j++) {
                System.out.print(deal[i][j]+"\t");
            }
        }
    }
}
